package com.atguigu.gmall.product.service.impl;


import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategory3TrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class baseTrademarkServiceImpl extends ServiceImpl<BaseTrademarkMapper,BaseTrademark> implements BaseTrademarkService {

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    @Autowired
    private BaseCategory3TrademarkMapper baseCategory3TrademarkMapper;

    //品牌管理分页列表
    @Override
    public IPage getTrademarkList(Page<BaseTrademark> page1) {
        QueryWrapper<BaseTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return baseTrademarkMapper.selectPage(page1, queryWrapper);
    }

    //根据category3Id获取品牌列表
    @Override
    public List<BaseTrademark> findTrademarkList(long category3Id) {
        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id",category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategory3TrademarkMapper.selectList(queryWrapper);

        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)) {
            List<Long> list = baseCategoryTrademarkList.stream().map((baseCategoryTrademark) -> {
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());
            return baseTrademarkMapper.selectBatchIds(list);
        }
        return null;
    }


    //删除分类品牌关联
    @Override
    public void removes(long category3Id, long trademarkId) {
        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id", category3Id);
        queryWrapper.eq("trademark_id", trademarkId);
        baseCategory3TrademarkMapper.delete(queryWrapper);
    }

    //根据category3Id获取可选品牌列表
    @Override
    public List<BaseTrademark> findCurrentTrademarkList(long category3Id) {
        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id", category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategory3TrademarkMapper.selectList(queryWrapper);

        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
            List<Long> list = baseCategoryTrademarkList.stream().map((baseCategoryTrademark) -> {
               return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());

       return baseTrademarkMapper.selectList(null).stream().filter((BaseTrademark)->{
            return !list.contains(BaseTrademark.getId());
        }).collect(Collectors.toList());
    }
        return baseTrademarkMapper.selectList(null);
    }

    //保存分类品牌关联
    @Override
    public void saves(CategoryTrademarkVo categoryTrademarkVo) {
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();

        if (!CollectionUtils.isEmpty(trademarkIdList)){
            for (Long tmId : trademarkIdList) {
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setTrademarkId(tmId);
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                baseCategory3TrademarkMapper.insert(baseCategoryTrademark);
            }
        }
    }


}
